<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Service;

use App\Model\Setting;
use Exception;
use Hyperf\Cache\Annotation\Cacheable;

class SettingService
{
    #[Cacheable(prefix: 'setting:group', value: '#{group}', ttl: 60)]
    public static function getGroupSetting(string $group): array
    {
        $list = Setting::query()->where('groups', $group)->get();
        $setting = [];
        if ($list) {
            $list = $list->toArray();
            $setting = array_combine(array_column($list, 'name'), array_column($list, 'value'));
        }
        return $setting;
    }

    #[Cacheable(prefix: 'setting:get', value: '#{name}', ttl: 600)]
    public static function get(string $name, mixed $default = ''){
        try {
            $set = Setting::query()->where('name', $name)->first();
            return $set ? $set['value'] : $default;
        } catch (Exception $e) {
            return $default;
        }
    }
}
